package com.onlinefurnitureshop.taglib;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.springframework.util.CollectionUtils;

import com.onlinefurnitureshop.management.model.Resource;
import com.onlinefurnitureshop.management.model.Role;
import com.onlinefurnitureshop.management.service.ResourceService;
import com.onlinefurnitureshop.management.service.RoleService;
import com.onlinefurnitureshop.myUtil.SpringUtils;

public class Functions {
	public static String resourceNames(Collection<Long> resourceIds){
		if(CollectionUtils.isEmpty(resourceIds))
			return "";
		StringBuilder sb =new StringBuilder();
		for(Long resourceId:resourceIds){
			Resource resource = getResourceService().findOne(resourceId);
			if(resource==null)
				return "";
			sb.append(resource.getName());
			sb.append("/");
		}
		if(sb.length()>0)
			sb.deleteCharAt(sb.length()-1);
		System.out.println("hello"+sb.toString());
		return sb.toString();
	}
	
	public static String roleName(Long roleId){
		Role role = getRoleService().findOne(roleId);
		if(role == null)
			return "";
		return role.getRole();
	}
	
	
	private static RoleService roleService;
	private static ResourceService resourceService;
	
	public static RoleService getRoleService(){
		if(roleService==null)
			roleService = SpringUtils.getBean("roleServiceImpl");
		return roleService;
	}
	
	public static ResourceService getResourceService(){
		if(resourceService==null)
			resourceService=SpringUtils.getBean("resourceServiceImpl");
		return resourceService;
	}
	
	public static List<Resource> findDescendants(Long id){
		List<Resource> menuList = new ArrayList<>();
		menuList = resourceService.findMenusByPid(id);
		return menuList;
	}
}
